<template>
    <el-drawer title="我是标题" :visible.sync="dialogVisible" :with-header="false" class="demand_box" size='720px' @close="resetForm">
        <div class="flexbox flexcolumn height_100">
            <div class="demand_head flexbox flexcenter flexbetween">
                <span class="demand_title">选择商品</span>
                <span class="icon_btn icon_btn_32" @click="dialogVisible=false"><img src="../../assets/img/close_check.svg" alt=""></span>
            </div>
            <div class="shop_search">
                <el-input placeholder="请输入商品名称/ID" size="small" style="width:300px" prefix-icon="el-icon-search" clearable @change="getShopList(1)" v-model="queryInfo.query">
                </el-input>
            </div>
            <div class="demand_center flex" v-loading="loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0)">
                <el-table :data="tableData" ref="goods_ref" height="100%" key="goods" class="com_table" :row-key="(row)=>{ return row.product_id}" @selection-change="selectGoods">
                    <div slot="empty" class="flexbox flexcenter flexcolumn flexjust" style="height:100%">
                        <img src="../../assets/img/null_icon.svg" alt="">
                        <div class="font_13 color_03">当前没有商品可供选择</div>
                    </div>
                    <el-table-column type="selection" width="55" align="center" :reserve-selection="true">
                    </el-table-column>
                    <el-table-column prop="product_name" label="商品名称">
                        <template slot-scope="scope">
                            <div class="flexbox flexcenter">
                                <img :src="scope.row.img" alt="" class="shop_img">
                                <div class="shop_name">{{scope.row.product_name}}</div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="product_id" label="商品 ID">
                    </el-table-column>
                </el-table>
            </div>
            <div class="page_box flexbox flexcenter" v-if="total>0">
                <el-pagination background layout="total, prev, pager, next,sizes" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.page" :page-size="queryInfo.limit" :page-sizes="[20, 50, 100, 200]" :total="total">
                </el-pagination>
            </div>
            <div class="footer_btn">
                <el-button size="small" @click="resetForm()" class="el-button-width">取 消</el-button>
                <el-button size="small" type="primary" @click="submitForm()" class="el-button-width">添 加</el-button>
            </div>
        </div>
    </el-drawer>
</template>

<script>
export default {
    props: ["editForm"],
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            hasCheck: [],
            queryInfo: {
                page: 1,
                limit: 20,
                query: "",
                advertiser: [],
            },
            total: 0,
            tableData: [],
        };
    },
    created() {},

    methods: {
        getHasSelect() {
            this.$nextTick(() => {
                this.editForm.product.forEach((row) => {
                    this.$refs.goods_ref.toggleRowSelection(row, true);
                });
            });
            
            this.getShopList(1);
        },

        //获取商品
        async getShopList(page) {
            this.queryInfo.page = page;
            this.queryInfo.advertiser = this.editForm.advertiser.map((item) => {
                return item.advertiser_id;
            });
            this.loading = true;
            const { data: res } = await this.$http.post(
                "fxdata/revenue/investProductList",
                this.queryInfo
            );
            if (res.code !== 200) {
                this.loading = false;
                this.$message.error(res.msg);
                return;
            } else {
                this.loading = false;
                this.total = res.data.count;
                this.tableData = res.data.list;
            }
        },

        // 表格选中
        selectGoods(v) {
            this.hasCheck = v;
        },

        submitForm() {
            this.editForm.product = this.hasCheck
            this.resetForm();
        },

        // 关闭重置表单
        resetForm() {
            this.setSessionStorage()
            this.queryInfo.query = "";
            this.$refs.goods_ref.clearSelection();
            this.dialogVisible = false;
        },

        // 每页显示多少条数据
        handleSizeChange(val) {
            this.queryInfo.limit = val;
            this.getShopList(1);
        },
        // 当前页切换
        handleCurrentChange(val) {
            this.setpage = val;
            this.getShopList(val);
        },

        setSessionStorage(){
            this.$emit("setSessionStorage")
        }
    },
    components: {},
};
</script>
<style scoped>
.shop_search {
    padding: 20px 20px 0 20px;
}
.shop_img {
    width: 48px;
    height: 48px;
    border-radius: 2px;
}
.shop_name {
    font-size: 14px;
    color: #17233d;
    margin-left: 20px;
}
.footer_btn {
    padding: 0 20px 20px 20px;
    text-align: right;
}
.demand_center{
    padding-bottom: 0;
}
</style>


